package com.xhlj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xhlj.annotation.SystemLog;
import com.xhlj.entity.UserInfo;
import com.xhlj.service.UserInfoService;
import com.xhlj.service.impl.LoginService;
import com.xhlj.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RequestMapping("user")
@RestController
public class UserController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private LoginService loginService;

    /*获取用户信息*/
    @GetMapping("userinfo")
    public Result getUserInfo() {
//        测试jwt登录的时候可以通过Security上下文来回去Authentications
        SecurityContext context = SecurityContextHolder.getContext();
        String username = context.getAuthentication().getName();
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserPhone, username);
        UserInfo userInfo = userInfoService.getOne(wrapper);
        return Result.ok(userInfo);
    }

    /*更新个人信息*/
    @SystemLog(title = "用户模块", content = "更新个人信息")
    @PutMapping
    public Result updateUserInfo(@RequestBody UserInfo userInfo) {
        LambdaUpdateWrapper<UserInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserInfo::getUserPhone, userInfo.getUserPhone());
        boolean update = userInfoService.update(wrapper);
        if (update) {
            return Result.build(null, 200, "修改信息成功");
        }
        return Result.build(null, 400, "修改信息失败");

    }


    /*
     * 通过redis来设置一个签到的key
     * 通过判断key是否存在来判断签到的状态, 签到以后重删掉重新设置一个key
     * *//*

    @PostMapping("sign")
    public Result sign(String username) {

        Result res = userInfoService.sign(username);
        return Result.build(null, 200, "签到成功");
    }
    */
    @SystemLog(title = "用户模块", content = "退出登录")
    @GetMapping("/logout")
    public Result logout(@RequestHeader String Token) {

        Result res = loginService.logout(Token);
        return res;
    }
}
